package com.csst.business.dao.mysql.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.csst.business.dao.INewsDao;
import com.csst.business.dao.TestDao;
import com.csst.business.model.BOnline;
import com.csst.business.model.TNews;
import com.csst.business.model.TTrUser;
import com.csst.popular.bean.message.NewsMessage.News;

public class NewsDaoImpl extends BaseDaoImpl implements INewsDao {

	@Override
	public void saveDate(TNews news) {
		Session session = getSessionFactory().getCurrentSession();
		session.save(news);
		session.flush();
		session.clear();
	}

	@Override
	public List<TNews> getNewslist() {
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from TNews t where t.SState = '1' order by t.SCreatetime";
		List<TNews> newslist = session.createQuery(sql).list();
		session.flush();
		session.clear();
		return newslist;
	}

	@Override
	public int getOrderTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TNews t where t.SState = '1' order by t.SCreatetime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public List<TNews> getAllOnlineList(int startNum, int endNum) {
		// TODO Auto-generated method stub
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from TNews t where t.SState = '1' order by t.SCreatetime";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<TNews> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public TNews getNewsById(String id) {
		Session session = getSessionFactory().getCurrentSession();
		TNews news=(TNews)session.get(TNews.class, id);
		return news;
	}

	@Override
	public void updateDate(TNews news) {
		Session session = getSessionFactory().getCurrentSession();
		session.update(news);
		session.flush();
		session.clear();
		
	}

	@Override
	public boolean deleteNewsById(String id) {
		Session session=getSessionFactory().getCurrentSession();
		String hql=" delete from TNews t   where t.id='"+id+"'";
		session.createQuery(hql).executeUpdate();
		session.flush();
		session.clear();
		return true;
	}


}
